import { drugsPageList } from '../../api/drugs'


Page({
  data: {
    current: 0,
    autoplay: true,
    duration: 500,
    interval: 5000,
    swiperList: [
      {
        image: 'https://inquiry-xcx.oss-cn-beijing.aliyuncs.com/swiper1.png',
        value: '0'
      },
      {
        image: 'https://inquiry-xcx.oss-cn-beijing.aliyuncs.com/swiper2.png',
        value: '1'
      },
      {
        image: 'https://inquiry-xcx.oss-cn-beijing.aliyuncs.com/swiper2.png',
        value: '2'
      }
    ],
    medicineList: [],
    currentPage: 0,
    hasMore: true,
    isLoading: false
  },

  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 'index'
      });
    }
  },

  onChange(e) {
    const { current } = e.detail;
    this.setData({ current });
  },

  onFindDoctor() {
    wx.showModal({
      title: '提示',
      content: '找医生',
      showCancel: false
    });
  },

  onMedicine() {
    wx.showModal({
      title: '提示',
      content: '送药上门',
      showCancel: false
    });
  },

  onMedicineItemClick(e) {
    const id = e.currentTarget.dataset.id;
    const medicine = this.data.medicineList.find(item => item.id === id);
    wx.showToast({
      title: `点击了${medicine.name}`,
      icon: 'none'
    });
  },

  onLoad() {
    // this.fetchSwiperList();
    this.fetchMedicineList();
  },

  // 获取轮播图数据
  fetchSwiperList() {
    wx.request({
      url: 'https://your-api-url/swiper/list', // 替换为实际的API地址
      method: 'GET',
      success: (res) => {
        if (res.statusCode === 200 && res.data.code === 200 && res.data.data) {
          // 假设返回数据格式为 { code: 200, data: [{ image: 'url', value: 'id' }], message: '' }
          const swiperData = res.data.data;
          if (swiperData && swiperData.length > 0) {
            this.setData({
              swiperList: swiperData
            });
          }
        } 
      },fail: (err) => {
        
      }
    });
  },

  // 获取药品列表数据
  async fetchMedicineList(currentPage = 0) {
    if (!this.data.hasMore || this.data.isLoading) return;
    
    this.setData({ isLoading: true });
    try {
      let res = await drugsPageList({ currentPage, pageSize: 10 });
      
      // 判断是否还有更多数据
      const hasMore = res.data && res.data.list.length === 10;
      
      this.setData({
        medicineList: currentPage === 0 
          ? res.data.list // 第一页直接覆盖
          : this.data.medicineList.concat(res.data.list), // 后续页追加
        currentPage: currentPage,
        hasMore,
        isLoading: false
      });
    } catch (error) {
      console.error('获取药品列表失败:', error);
      this.setData({ 
        isLoading: false,
        hasMore: false 
      });
      wx.showToast({
        title: '获取药品列表失败',
        icon: 'none'
      });
    }
  },

  // 加载更多数据
  onLoadMore() {
    if (this.data.hasMore && !this.data.isLoading) {
      this.fetchMedicineList(this.data.currentPage + 1);
    }
  },

  goToDescribeIllness() {
    wx.navigateTo({
      url: '/pages/describe-illness/index'
    })
  },


  // 添加搜索栏聚焦事件处理
  onSearchFocus() {
    wx.navigateTo({
      url: '/pages/search/index'
    });
  },

  handleFindDoctor() {
    wx.navigateTo({
      url: '/pages/find-doctor/index',
      fail: (err) => {
        console.error('跳转失败：', err);
        wx.showToast({
          title: '页面跳转失败',
          icon: 'none'
        });
      }
    });
  }
});
